<template>
  <van-loading v-if="loading" size="0.5rem" />
  <van-icon
    v-else
    :color="value ? '#ffa500' : '#777'"
    :name="value ? 'star' : 'star-o'"
    @click="onCollect"
  />
</template>
  
  <script>
import { addCollect, deleteCollect } from "@/api/article";
export default {
  name: "CollectArticle",
  data(){
    return {
      loading:false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    id: {
      type: [String, Number],
      required: true,
    },
  },
  methods: {
    async onCollect() {
      try {
        this.loading = true
        console.log("点击了收藏或取消收藏");
      if (this.value) {
        await deleteCollect(this.id);
      } else {
        await addCollect(this.id);
      }
      this.$emit("input", !this.value); 
      } finally {
        this.loading = false
      }
  
    },
  },
};
</script>
  